package admin

import (
	"baokaobang/datasourse"
	"baokaobang/models"
	"baokaobang/utils"
	"encoding/json"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
	"strconv"
	"strings"
)

type AddCategory struct {
	Id   string `json:"id"`
	Name string `json:"Name"`
}

type CategoryNamePram struct {
	Name string `json:"name"`
}

type CategoryPram struct {
	Name    string `json:"name"`
	PerPage int    `json:"perPage"`
	CurPage int    `json:"curPage"`
}

type CategoryController struct {
	Ctx iris.Context
}

func (c *CategoryController) OptionsAll() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

func (c *CategoryController) PostAll() interface{} {
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	categoryList := make([]models.Category, 0)
	err = db.Find(&categoryList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取分类列表:%s", err.Error()), "")
	}

	return utils.CommonRes(20000, "成功获取分类列表", categoryList)
}

func (c *CategoryController) OptionsCreate() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

func (c *CategoryController) PostCreate() interface{} {
	addCategory := AddCategory{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析前端参数出错：%s", err.Error()), "")
	}
	err = json.Unmarshal([]byte(content), &addCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析参数出错:%s", err.Error()), "")
	}
	if len(addCategory.Name) <= 0 {
		return utils.CommonRes(1000, "请输入分类名", "")
	}
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库%s", err.Error()), "")
	}

	category := new(models.Category)
	category.Name = addCategory.Name

	_, err = db.InsertOne(category)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("操作失败%s", err.Error()), "")
	}
	return utils.CommonRes(20000, "操作成功", "")
}

func (c *CategoryController) OptionsEdit() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

func (c *CategoryController) PostEdit() interface{} {
	addCategory := AddCategory{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	err = json.Unmarshal([]byte(content), &addCategory)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析参数出错:%s", err.Error()), "")
	}
	if len(addCategory.Id) <= 0 {
		return utils.CommonRes(1000, "缺少必要参数", "")
	}
	if len(addCategory.Name) <= 0 {
		return utils.CommonRes(1000, "请输入分类名", "")
	}
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}

	category := new(models.Category)
	category.Id, _ = strconv.ParseInt(addCategory.Id, 10, 64)
	category.Name = strings.TrimSpace(addCategory.Name)
	_, err = db.Table(new(models.Category)).ID(addCategory.Id).Update(category)

	if err != nil {
		return utils.CommonRes(1000, "操作失败", "")
	}

	return utils.CommonRes(20000, "操作成功", "")
}

func (c *CategoryController) OptionsDel() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

func (c *CategoryController) PostDel() interface{} {
	delParam := utils.DelParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接受请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &delParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	if len(delParam.Ids) <= 0 {
		return utils.CommonRes(1000, "缺少必要参数", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接服务器:%s", err.Error()), "")
	}
	ids := strings.Split(delParam.Ids, ",")
	for _, id := range ids {
		category := new(models.Category)
		article := new(models.Article)
		total, err := db.Where("cid=?", id).Count(article)
		if err != nil {
			return utils.CommonRes(1000, "无效的分类", "")
		}
		if total > 0 {
			return utils.CommonRes(1000, "有文章关联该分类，暂时无法删除", "")
		}

		db.ID(id).Get(category)
		db.Id(id).Delete(category)
	}
	return utils.CommonRes(20000, "删除成功", "")
}

func (c *CategoryController) OptionsList() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

func (c *CategoryController) PostList() interface{} {
	categoryParam := CategoryPram{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}

	err = json.Unmarshal([]byte(content), &categoryParam)
	if err != nil {
		return mvc.Response{
			Object: utils.CommonRes(1000, fmt.Sprintf("解析参数出错:%s", err.Error()), ""),
		}
	}
	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, "无法连接服务器", "")
	}
	if categoryParam.CurPage <= 0 {
		categoryParam.CurPage = 1
	}
	if categoryParam.PerPage <= 0 {
		categoryParam.PerPage = 10
		categoryParam.PerPage = 10
	}
	start := (categoryParam.CurPage - 1) * categoryParam.PerPage
	sess := db.Limit(categoryParam.PerPage, start)
	if len(categoryParam.Name) > 0 {
		sess = sess.Where("name like ? ", fmt.Sprintf("%%%s%%", categoryParam.Name))
	}
	categoryList := make([]models.Category, 0)
	err = sess.Find(&categoryList)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取大学标签列表:%s", err.Error()), "")
	}
	if len(categoryParam.Name) > 0 {
		sess = sess.Where("name like ? ", fmt.Sprintf("%%%s%%", categoryParam.Name))
	}
	total, err := sess.Count(new(models.Category))
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法获取大学标签列表:%s", err.Error()), "")
	}
	data := map[string]interface{}{
		"total": total,
		"list":  categoryList,
	}

	return utils.CommonRes(20000, "成功获取大学标签列表", data)
}

func (c *CategoryController) OptionsDetail() mvc.Result {
	return mvc.Response{
		Code: 204,
	}
}

func (c *CategoryController) PostDetail() interface{} {
	editParam := utils.EditParam{}
	content, err := c.Ctx.GetBody()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("接收请求参数出错:%s", err.Error()), "")
	}
	err = json.Unmarshal(content, &editParam)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("解析请求参数出错:%s", err.Error()), "")
	}
	if len(editParam.Id) <= 0 {
		return utils.CommonRes(1000, "参数不完整", "")
	}

	db, err := datasourse.NewMasterOrm()
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("无法连接数据库:%s", err.Error()), "")
	}
	category := new(models.Category)
	ok, err := db.Id(editParam.Id).Get(category)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("信息获取失败:%s", err.Error()), "")
	}
	if !ok {
		return utils.CommonRes(1000, "信息获取失败", "")
	}
	return utils.CommonRes(20000, "信息获取成功", category)
}
